package tn.insat.gl4.ppp;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import greendroid.widget.ActionBarItem.Type;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DataGreedMenuActivity extends GDActivity {
	/** Called when the activity is first created. */
	private final int INFO = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.main);
		initActionBar();
		getActionBar().setType(ActionBar.Type.Normal);
		GridView g = (GridView) findViewById(R.id.myGrid);
		g.setAdapter(new ImageAdapter(this));
		g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Toast.makeText(DataGreedMenuActivity.this, "" + position,
						Toast.LENGTH_SHORT).show(); // TODO remove Toast
				switch (position) {
				case 0: // TODO OnClick Title0
					break;
				case 1: // TODO OnClick Title1
					break;
				case 2: // TODO OnClick Title2
					break;
				case 3: // TODO OnClick Title3
					break;
				case 4: // TODO OnClick Title4
					break;
				case 5: // TODO OnClick Title5
					break;
				default: // TODO OnClick Title
					break;
				}
			}
		});
	}

	/**
	 * Inialize the Action Bar with info Button.
	 */
	private void initActionBar() {
		addActionBarItem(Type.Info, INFO);
	}

	/**
	 * handling le event of click on button in ActionBar.
	 */
	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		switch (item.getItemId()) {
		case INFO:
			Intent intent = new Intent(DataGreedMenuActivity.this,
					InfoOfDevelopers.class);
			startActivity(intent);
			break;
		default:
			return super.onHandleActionBarItemClick(item, position);
		}
		return true;
	}

	/**
	 * @author gasmi houcine Use this Adapter to create dynamic DataGreed Menu
	 *         with images and a title.
	 */
	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			LinearLayout linearLayout = null;
			if (convertView == null) {
				linearLayout = new LinearLayout(mContext);
				linearLayout.setOrientation(LinearLayout.VERTICAL);
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
				imageView.setAdjustViewBounds(false);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				// imageView.setPadding(8, 16, 8, 16);
				linearLayout.addView(imageView);
				imageView.setImageResource(mThumbIds[position]);
				TextView textView = new TextView(mContext);
				setTextForTextView(textView, position);
				textView.setGravity(Gravity.CENTER);
				linearLayout.addView(textView);
				linearLayout.setGravity(Gravity.CENTER);
			} else {
				linearLayout = (LinearLayout) convertView;
			}
			return linearLayout;
		}

		private void setTextForTextView(TextView textView, int position) {
			String title = null;
			switch (position) {
			case 0:
				title = "Title" + 0; // TODO Change Title0
				break;
			case 1:
				title = "Title" + 1; // TODO Change Title1
				break;
			case 2:
				title = "Title" + 2; // TODO Change Title2
				break;
			case 3:
				title = "Title" + 3; // TODO Change Title3
				break;
			case 4:
				title = "Title" + 4; // TODO Change Title4
				break;
			case 5:
				title = "Title" + 5; // TODO Change Title5
				break;
			default:
				title = "Title"; // TODO Change Title
				break;
			}
			textView.setText(title);
		}

		private Context mContext;
		private Integer[] mThumbIds = { R.drawable.ic_launcher,
				R.drawable.ic_launcher, R.drawable.ic_launcher,
				R.drawable.ic_launcher, R.drawable.ic_launcher,
				R.drawable.ic_launcher, };
	}
}